<?php
	session_start();
	header("Content-Type: text/html;charset=utf-8");
	if(isset($_SESSION['quid'])&&$_SESSION['quid']=="start"&&isset($_SESSION['thisevent'])&&isset($_GET['type'])&&$_GET['type']=="logout"){//登录了并且有本次事件并且要退出
		//创建连接
		$thisevent=$_SESSION['thisevent'];
		$conn = mysqli_connect("localhost", "root", "", "signin");
		mysqli_query($conn,"set names utf8");
		//检测连接
		if (!$conn){
			die("Connection failed:".mysqli_connect_error());
		}
		$sql2="update persons set username='end' where event='$thisevent' AND username='start'";
		$result = mysqli_query($conn,$sql2);//处理数据库
		mysqli_close($conn);
		session_destroy();
		echo "<script>alert('结束签到成功！');location.href='index.php';</script>";
		exit();
	}
	if(isset($_POST['adminname'])&&isset($_POST['adminpass'])&&$_POST['adminpass']=="admin1pass"&&isset($_POST['eventname'])){
		$eventname=$_POST['eventname'];
		$adminname=$_POST['adminname'];
		unset($_POST['adminname']);
		unset($_POST['adminpass']);
		if($adminname=="view"){//查看结果
			echo "<title>查询结果</title>";
			$conn = mysqli_connect("localhost", "root", "", "signin");
			mysqli_query($conn,"set names utf8");
			//检测连接
			if (!$conn){
				die("Connection failed:".mysqli_connect_error());
			}
			if($eventname=="all"){//查看所有
				$result = mysqli_query($conn,"select * from persons"); //执行SQL查询指令
			}else{
				$result = mysqli_query($conn,"select * from persons WHERE event='$eventname'");
			}
			echo "<center>\n<table border=1>\n<tr>\n";
			while($field = mysqli_fetch_field($result)){//使用while输出表头
				echo "<td>&nbsp;".$field->name."&nbsp;</td>\n";
			}
			echo "<td>&nbsp;操作&nbsp;</td>\n";
			echo"</tr>\n";
			$signinnum=0;
			while($rows = mysqli_fetch_row($result)){//使用while遍历所有记录，并显示在表格的tr中
				if($signinnum>0){
					echo "<tr>\n";
					for($i = 0; $i < count($rows); $i++)
						echo "<td>&nbsp;".$rows[$i]."&nbsp;</td>\n";
					echo "<td>&nbsp;<a href=\"javascript:dodel({$rows[0]})\">删除</a>&nbsp;</td>\n";
					echo "</tr>\n";
				}
				$signinnum++;
			}
			echo "</table>\n";
			echo "<br/><br/>";
			if($signinnum==0){
				echo "事件\"".$eventname."\"未曾发生过！";
			}else{
				echo "事件\"".$eventname."\"中总共有".--$signinnum."人完成签到！";
			}
			echo "</center>\n";
			exit();
		}elseif($adminname=="start"){//如果是创建事件
			//创建连接
			$conn = mysqli_connect("localhost", "root", "", "signin");
			mysqli_query($conn,"set names utf8");
			//检测连接
			if (!$conn){
				die("Connection failed:".mysqli_connect_error());
			}
			$eventname = mysqli_real_escape_string($conn,$eventname);
			$sql1 = "SELECT * FROM persons WHERE event='$eventname'";
			$result1 = mysqli_query($conn, $sql1);
			if($row = mysqli_fetch_assoc($result1)){//如果事件存在
				echo '<script>alert("对不起，事件“'.$eventname.'”已存在，请换个名字");location.href="adminlogin.php";</script>';
				exit();
			}else{
				$code = mt_rand(100000,999999); 
				$freshtime=time();
				$sql = "INSERT INTO persons (username, code, add_time, event) 
				VALUES ('start', '$code', '$freshtime', '$eventname')";
				$result = mysqli_query($conn, $sql);
				mysqli_close($conn);
				$_SESSION['quid']="start";
				$_SESSION['thisevent']=$eventname;
				echo '<script>alert("好了，事件“'.$eventname.'”已设置完成，马上就可以开始了！");location.href="index.php";</script>';
				exit();
			}
		}elseif($adminname=="end"){//如果是结束事件（一般不用）
			//创建连接
			$conn = mysqli_connect("localhost", "root", "", "signin");
			mysqli_query($conn,"set names utf8");
			//检测连接
			if (!$conn){
				die("Connection failed:".mysqli_connect_error());
			}
			$eventname = mysqli_real_escape_string($conn,$eventname);
			$sql1 = "SELECT * FROM persons WHERE event='$eventname' AND username='start'";//如果该事件存在并开始着
			$result1 = mysqli_query($conn, $sql1);
			if($row = mysqli_fetch_assoc($result1)){//如果事件存在
				$sql2="update persons set username='end' where event='$eventname' AND username='start'";
				$result = mysqli_query($conn,$sql2);//处理数据库
				mysqli_close($conn);
				echo '<script>alert("成功结束事件“'.$eventname.'”的签到状态！");location.href="adminlogin.php";</script>';
				exit();
			}else{
				mysqli_close($conn);
				echo '<script>alert("事件“'.$eventname.'”并不存在或已结束，结束失败！");location.href="adminlogin.php";</script>';
				exit();
			}
		}
	}else{
?>
<html>
<head>
<meta charset="utf-8">
<title>管理员扫码系统登录</title>
</head>
<body>
	<center>
	<br/><br/>
	<h3>管理员登录</h3>
    <form action="adminlogin.php" method="POST" onsubmit="return CheckValue();">
		事件名称：<input maxlength="16" onpaste="return false" name="eventname" id="eventname" onKeypress="javascript:if(event.keyCode == 32)event.returnValue = false;" type="text" class="kuang_txt" placeholder="事件名称"><br/><br/>
        　用户名：<input maxlength="16" onpaste="return false" name="adminname" id="loginname" onKeypress="javascript:if(event.keyCode == 32)event.returnValue = false;" type="text" class="kuang_txt" placeholder="用户名"><br/><br/>
        　　密码：<input maxlength="16" onpaste="return false" name="adminpass" id="loginpwd" onKeypress="javascript:if(event.keyCode == 32)event.returnValue = false;" type="password" class="kuang_txt" placeholder="密码"><br/><br/>
        <input name="登录" type="submit" class="btn_zhuce" value="登录">
    </form>
	</center>
</body>
</html>
<?php
	}
?>